package cn.uniquetech;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.uniquetech.flex.FlexUtil;
import cn.uniquetech.vo.MySqlTableVO;

public class Flex {

	private static final String URL = "jdbc:mysql://192.168.0.118:3306/jhq";
	private static final String DBNAME = "jhq";
	private static final String NAME = "root";
	private static final String PASS = "root";
	private static final String DRIVER = "com.mysql.jdbc.Driver";

	public static void main(String[] args) {

		List<MySqlTableVO> fieldList = new ArrayList<MySqlTableVO>();

		List<String> tableNames = new ArrayList<String>();
		List<String> voNames = new ArrayList<String>();

		tableNames.add("deviceprices");
		voNames.add("DevicePrice");

		try {
			Class.forName(DRIVER);
			Connection con = DriverManager.getConnection(URL, NAME, PASS);
			Statement ps = null;

			for (int i = 0; i < tableNames.size(); i++) {
				String tableName = tableNames.get(i);
				String voName = voNames.get(i);
				String sql = "select case when character_maximum_length is null then 0 else character_maximum_length end clen,column_name,ordinal_position,is_nullable,data_type,column_comment,column_default,(select table_comment from information_schema.tables " + "where table_schema = '" + DBNAME + "' and table_name='" + tableName.trim() + "') tableComment from information_schema.columns "
						+ "where table_schema = '" + DBNAME + "' and table_name='" + tableName.trim() + "' order by ordinal_position asc";
				ps = con.createStatement();
				// System.out.println(sql);
				ResultSet rs = ps.executeQuery(sql);
				MySqlTableVO tableVO = null;
				while (rs.next()) {
					tableVO = new MySqlTableVO();
					tableVO.setTableName(tableName.trim());
					tableVO.setVoName(voName.trim());
					tableVO.setClen(rs.getInt("clen"));
					tableVO.setColName(rs.getString("column_name").toLowerCase().trim());
					tableVO.setSort(rs.getInt("ordinal_position"));
					tableVO.setIsNull(rs.getString("is_nullable"));
					tableVO.setDataType(rs.getString("data_type"));
					tableVO.setComment(rs.getString("column_comment"));
					tableVO.setColDefault(rs.getString("column_default"));
					tableVO.setTableComment(rs.getString("tableComment"));
					fieldList.add(tableVO);
				}
				if (tableVO != null) {
					//insertMenu(ps, tableVO.getTableComment(), "cn.uniquetech.view." + voName.toLowerCase() + "." + voName + "View", tableVO.getTableComment());
				}

			}

			FlexUtil.sc(fieldList);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void insertMenu(Statement ps, String cname, String path, String comment) throws Exception {
		//cname = new String(cname.getBytes("ISO-8859-1"),"UTF-8");
		//comment = new String(comment.getBytes("ISO-8859-1"),"UTF-8");
		String sql = "insert into menus (ipid,cname,sort,path,comment,adduid,adduname,adddate,edituid,edituname,editdate) values (1,'" + cname + "',1,'" + path + "','" + comment + "',2,'admin',now(),2,'admin',now())";
		int count = ps.executeUpdate(sql);
		if(count > 0){
			sql = "select iid from menus order by iid desc limit 0,1";
			ResultSet rs = ps.executeQuery(sql);
			int newiid = 0;
			while (rs.next()) {
				newiid = rs.getInt("iid");
			}
			if(newiid !=0){
				sql = "insert into rolemenus (roleid,menuid,state) values (1,"+newiid+",1) ";
				ps.execute(sql);
				System.out.println("sql:"+sql);
			}
			
		}
	}

}
